package de.dubiel.plung.client.pages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.Widget;

import de.dubiel.plung.client.gin.ClientGinjector;
import de.dubiel.plung.client.pages.account.AccountPlace;
import de.dubiel.plung.client.pages.game.GamePlace;
import de.dubiel.plung.client.pages.start.StartPlace;

public class MainPage extends Composite implements AcceptsOneWidget {

    private static MainPageUiBinder uiBinder = GWT.create(MainPageUiBinder.class);

    interface MainPageUiBinder extends UiBinder<Widget, MainPage> {
    }

    @UiField
    MenuBar menuBar;

    @UiField
    HTMLPanel placeContainer;

    private final ClientGinjector clientFactory;

    public MainPage(ClientGinjector clientFactory) {
        initWidget(uiBinder.createAndBindUi(this));
        this.clientFactory = clientFactory;
        renderMenuBar();
    }

    @Override
    public void setWidget(IsWidget w) {
        if (w == null)
            return;
        placeContainer.clear();
        placeContainer.add(w);
    }

    private void renderMenuBar() {
        ScheduledCommand cmd = new ScheduledCommand() {
            @Override
            public void execute() {
                MainPage.this.clientFactory.getPlaceController().goTo(new StartPlace());
            }
        };
        menuBar.addItem("Start", cmd);
        cmd = new ScheduledCommand() {
            @Override
            public void execute() {
                MainPage.this.clientFactory.getPlaceController().goTo(new GamePlace());
            }
        };
        menuBar.addItem("Spiel", cmd);
        cmd = new ScheduledCommand() {
            @Override
            public void execute() {
                MainPage.this.clientFactory.getPlaceController().goTo(new AccountPlace());
            }
        };
        menuBar.addItem("Account", cmd);
    }
}
